package com.dmbjz.consumer;


import com.alibaba.fastjson.JSON;
import com.dmbjz.config.RabbitConfig;
import com.dmbjz.dao.ProductDao;
import com.dmbjz.dao.RestaurantDao;
import com.dmbjz.entity.dto.OrderMessageDTO;
import com.dmbjz.entity.po.ProductPO;
import com.dmbjz.entity.po.RestaurantPO;
import com.dmbjz.enummeration.ProductStatus;
import com.dmbjz.enummeration.RestaurantStatus;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import static com.dmbjz.enummeration.OrderStatus.*;

/* 餐厅监听器 */
@Component
public class RestaurantConsumer {

    @Autowired
    private ProductDao productDao;
    @Autowired
    private RestaurantDao restaurantDao;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /* 处理订单 */
    @RabbitListener(queues = RabbitConfig.RESTAURANT_QUEUE,containerFactory = "rabbitListenerContainerFactory")
    public void orderMessage(Message message, Channel channel) throws IOException {

        String bodyStr = new String(message.getBody());
        OrderMessageDTO messageDTO = JSON.parseObject(bodyStr, OrderMessageDTO.class);
        ProductPO productPO = productDao.selectProduct(messageDTO.getProductId());                  //获取数据库中的商品数据
        RestaurantPO restaurantPO = restaurantDao.selectRestaurant(productPO.getRestaurantId());    //通过商品获取对应的餐厅数据

        /* 判断当前店家是否已经打烊且订单对应商品库存是否充足，更新订单状态 */
        if(restaurantPO.getStatus() == RestaurantStatus.OPEN && productPO.getStatus() == ProductStatus.AVALIABLE ){
            messageDTO.setConfirmed(true);
            messageDTO.setOrderStatus(RESTAURANT_CONFIRMED);     //餐厅已确认
            messageDTO.setPrice(productPO.getPrice());
        }else{
            messageDTO.setOrderStatus(FALIED);   //订单创建失败
            messageDTO.setConfirmed(false);
        }

        /* 发送消息 */
        MessageProperties properties = new MessageProperties();
        properties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
        properties.setContentEncoding("utf-8");
        String jsonMsg = JSON.toJSONString(messageDTO);
        Message sendMsg = new Message(jsonMsg.getBytes(StandardCharsets.UTF_8),properties);

        CorrelationData correlationData = new CorrelationData();
        ReturnedMessage returnedMessage = new ReturnedMessage(sendMsg,300,"餐厅收到订单消息，发送给订单服务队列",
                RabbitConfig.RESTAURANT_EXCHANGE,
                "key.order");
        correlationData.setReturned(returnedMessage);
        correlationData.setId(messageDTO.getOrderID());     //设置为订单ID

        rabbitTemplate.convertAndSend(RabbitConfig.RESTAURANT_EXCHANGE,"key.order",sendMsg,correlationData);


        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);        //手动消息应答

    }



}
